/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.uoc.tfc.exuoc.model;
import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Version;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.annotations.Cascade;


import java.util.List;
import java.util.ArrayList;

import edu.uoc.tfc.exuoc.config.Config;

/**
 *  Model pels objectes Metadades
 * @author Emili A. Moreno
 */

@Entity
@Table(name="metesdublincore")
public class MetaDC implements Serializable , Comparable {
    private static final long serialVersionUID = 3245235352373L;

    private Long id=Config.valorNull;
    private String nom=Config.valorNullStr;  
    private String descripcio=Config.valorNullStr;
    private int versionNum=0;

    // E-R amb Items
    //Emili:16-01-2012:M-n:private List<Item> items=new ArrayList<Item>(0);
    List<MetadadaDetall> metadadesDetalls = new ArrayList<MetadadaDetall>();


    public MetaDC(){ }

    public MetaDC(String _nom) {
        this.nom = _nom;
    }

    public MetaDC(String _nom, String _descripcio) {
        this.nom = _nom;
        this.descripcio = _descripcio;
    }

    public MetaDC(Long _id, String _nom, String _descripcio) {
        this.emplenar(_id, _nom, _descripcio);
    }

    private void emplenar(Long _id,String _nom,String _descripcio){

        if (Long.valueOf(_id).equals(null)){  this.id=Config.valorNull;}
        else this.id=_id;

        if (_nom.equals(null)) this.nom=Config.valorNullStr;
        else this.nom=_nom;

        if (_descripcio.equals(null)) this.descripcio = Config.valorNullStr;
        else this.descripcio=_descripcio;

    }

    @Id
    @SequenceGenerator(name="metesdublincore",
                       sequenceName="seqmetesdublincore")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, 
                    generator="metesdublincore")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    
    @Column(length=254,nullable=false)
    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    @Column(length=254)
    public String getDescripcio() {
        return descripcio;
    }

    public void setDescripcio(String descripcio) {
        this.descripcio = descripcio;
    }



   // Control d'exceptions :StaleObjectStateException
    @Version
    @Column(name="version", nullable=true)
    protected int getVersionNum() {
        return versionNum;
    }

    protected void setVersionNum(int versionNum) {
        this.versionNum = versionNum;
    }



    @OneToMany(fetch = javax.persistence.FetchType.LAZY, mappedBy = "pk.metaDC",
    cascade ={javax.persistence.CascadeType.PERSIST,
        javax.persistence.CascadeType.MERGE})
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
    org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    public List<MetadadaDetall> getMetadadesDetalls() {
        return metadadesDetalls;
    }

    public void setMetadadesDetalls(List<MetadadaDetall> _metedadesDetalls) {
        this.metadadesDetalls = _metedadesDetalls;
    }

    public void addItem(Item item, String valorDC) {
        MetadadaDetall associacio = new MetadadaDetall();
        associacio.setItem(item);
        associacio.setMetaDC(this);
        associacio.setValor(valorDC);

        this.metadadesDetalls.add(associacio);
        // Also add the association object to the employee.
        item.getMetadadesDetalls().add(associacio);
    }

    public void delItem(Item item){
        MetadadaDetall associacio = new MetadadaDetall();
        associacio.setItem(item);
        associacio.setMetaDC(this);

        this.metadadesDetalls.remove(associacio);
        item.getMetadadesDetalls().remove(associacio);
    }



    //@OneToMany(cascade=javax.persistence.CascadeType.ALL,
    //fetch=javax.persistence.FetchType.EAGER, mappedBy="grup")

/* 16-01-2012:Emili:n-m
   @ManyToMany(mappedBy ="metesDC")
   // @OnDelete(action = OnDeleteAction.CASCADE)
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> _Items) {
        this.items = _Items;
    }

    public void addItem(Item _item)
    {
        //Ja el tenim ?
        if ( ! this.items.contains(_item))
            this.items.add((Item)_item);
    }

    public void delItem(Item _item)
    {
        //Existeix l'ítem?
        if (this.items.contains(_item))
            this.items.remove(_item);
    }
*/
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final MetaDC other = (MetaDC) obj;

        if (this.id !=other.id){
            return false;
        }
        if ((this.nom == null) ? (other.nom != null) : !this.nom.equals(other.nom)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.id != null ? this.id.hashCode() : 0 );
        hash = 97 * hash + (this.nom != null ? this.nom.hashCode() : 0);
        hash=97 * hash+(this.descripcio != null ? this.descripcio.hashCode():0);
        return hash;
    }


    @Override
    public String toString() {
        return id + ";"+nom+";"+descripcio;
    }

    @Override
    public int compareTo(Object o) {
        MetaDC i =null;
        if (o == null) {
            return -2;
        }
        if (!this.getClass().equals( o.getClass())) {
           return -2;
        }
        i=(MetaDC)o;

        return (this.equals(i))? 0:1;
    }

    public static void main (String[] args){

        MetaDC gp1;
        MetaDC gp2;

        gp1=new MetaDC();
        gp1.setId(1L);
        gp1.setNom("grup1");

        gp2=new MetaDC();
        System.out.println("- "+gp1.compareTo(gp2));
        gp2.setId(1L);
        gp2.setNom("grup1");
        System.out.println("- "+gp1.compareTo(gp2));
    }

}


